﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Business.AID;
using Business.AID.Extensions;
using Data.AID;
using Microsoft.SharePoint;
using System.Collections.Generic;

namespace Presentation.AID.WebPart.Presentation.AID.VWPConsultarMeta
{
    public partial class VWPConsultarMetaUserControl : UserControl
    {
        private List<Formulario> ListaFormulario
        {
            get { return Cache["ListFormulario"] as List<Formulario>; }
            set { Cache["ListFormulario"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetGridView();
            }
        }

        #region Botões

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            txtNome.Text = string.Empty;
            txtMatricula.Text = string.Empty;
            SetGridView();
        }
        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            FilterReport();
        }

        #endregion

        #region Métodos Privados

        private void FilterReport()
        {
            string _nome = txtNome.Text.RemoveAccents();
            string _matricula = txtMatricula.Text;

            ListaFormulario = GetSubordinateForms();

            if (_nome != "" || _matricula != "")
            {
                if (_nome != "")
                {
                    ListaFormulario = ListaFormulario.Where(p => p.Nome.ToLower().RemoveAccents().Contains(_nome.ToLower())).ToList();
                }

                if (_matricula != "")
                {
                    ListaFormulario = ListaFormulario.Where(p => p.Matricula.ToLower().Contains(_matricula.ToLower())).ToList();
                }
            }

            gdvFormulariosGestor.DataSource = ListaFormulario;
            gdvFormulariosGestor.DataBind();

        }
        private void SetGridView()
        {
            ListaFormulario = GetSubordinateForms();
            gdvFormulariosGestor.DataSource = ListaFormulario;
            gdvFormulariosGestor.DataBind();
        }
        private List<Formulario> GetSubordinateForms()
        {
            ColaboradorBO colaboradorBO = new ColaboradorBO();
            Colaborador colaborador = colaboradorBO.GetEmployeeByLogin(SPContext.Current.Web.CurrentUser.LoginName);
            FormularioBO formularioBO = new FormularioBO();
            return formularioBO.GetSubordinateForms(colaborador.Id).OrderBy(p => p.AprovacaoGestor).ToList();
        }
        #endregion

        #region Eventos

        protected void gdvFormulariosGestor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvFormulariosGestor.DataSource = ListaFormulario;
            gdvFormulariosGestor.PageIndex = e.NewPageIndex;
            gdvFormulariosGestor.DataBind();
        }
        
        #endregion

    }
}
